<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
                <!-- <a-row>
                    <a-col :span="24">
                        <a-form-model-item label="消费类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                            prop="consumptionType">
                            <j-dict-select-tag type="list" v-model="model.consumptionType" dictCode="consumption_type"
                                placeholder="请选择消费类型" disabled />
                        </a-form-model-item>
                    </a-col>
                </a-row> -->
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCode">
                            <a-input v-model="model.productCode" placeholder="请输入商品编码" :disabled="disableCode"></a-input>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
                            <a-input v-model="model.productName" placeholder="请输入商品名称"></a-input>
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
                            <a-input-number v-model="model.price" placeholder="请输入价格" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
                            <a-input v-model="model.unit" placeholder="请输入单位"></a-input>
                        </a-form-model-item>
                    </a-col>

                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="助记码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mnemo">
                            <a-input v-model="model.mnemo" placeholder="请输入助记码"></a-input>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="备注说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                            <a-input v-model="model.remark" placeholder="请输入备注说明"></a-input>
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isEnable">
                            <j-switch defaultChecked v-model="model.isEnable" :options="['1', '0']"></j-switch>
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-form-model>
        </j-form-container>
    </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
    name: 'RoomGoodsForm',
    components: {
    },
    props: {
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data() {
        return {
            disableCode: false,
            model: {
                consumptionType: '001'
            },
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            confirmLoading: false,
            validatorRules: {
                productCode: [{ required: true, message: '请完善内容' }],
                productName: [{ required: true, message: '请完善内容' }],
                price: [{ required: true, message: '请完善内容' }],
                unit: [{ required: true, message: '请完善内容' }],
            },
            url: {
                add: "/common/commonProductInfo/add",
                edit: "/common/commonProductInfo/edit",
                queryById: "/common/commonProductInfo/queryById"
            }
        }
    },
    computed: {
        formDisabled() {
            return this.disabled
        },
    },
    created() {
        //备份model原始值
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
        add() {
            this.edit(this.modelDefault);
        },
        edit(record) {
            if (record.productCode) {
                this.disableCode = true
            }
            this.model = Object.assign({}, record);
            this.visible = true;
        },
        submitForm() {
            const that = this;
            // 触发表单验证
            this.$refs.form.validate(valid => {
                if (valid) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    httpAction(httpurl, this.model, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    })
                }

            })
        },
    }
}
</script>